/*jslint vars: true, forin: true, sloppy: true */
/*global probe, angular */
probe.controller("ObjectUpdate", function ($scope, $element, ocDialog, Capture, GET_FRAME) {
	var DI = ocDialog.queryDI($element),
		share = DI.share
		;

	$scope.object = {
		name: '',
		cssPath: ''
	};

	if (share.edit) {
		$scope.object = share.objectList[share.selected];
	}
    
	$scope.updateObject = function () {
		if (share.edit) {
			share.objectList[share.selected] = $scope.object;
		} else {
			if (share.selected > -1) {
				share.objectList.splice(share.selected, 0 ,$scope.object);
			}
			//nothing is selected, append instruction at the end
			else {
				share.objectList.push($scope.object);
			}
		}

		share.refresh();

		DI.closeDialog();
	};
    
    $scope.capture = function () {
		GET_FRAME().style.backgroundColor = '#ffffff';
		Capture.setZIndex(550).setContext(GET_FRAME().contentWindow);
	    //remove the block on parent to fix a bug on IE
	    //hide it temporarily
	    DI.$$element.css({
		    "opacity": 0
	    });
	    //there is another parent...
	    DI.$$parent.$$parent.setBlock();
	    DI.$$parent.$$element.css({
		    "opacity": 0
	    });

		Capture.watchHTML(function (selector) {
			GET_FRAME().style.zIndex = 1;
			GET_FRAME().style.backgroundColor = '';
			DI.$$element.css({
				"opacity": 1
			});
			//reinstall the block
			//there is another parent...
			DI.$$parent.$$element.css({
				"opacity": 1
			});
			DI.$$parent.$$parent.setBlock(true);

			$scope.object.cssPath = selector;
		});
	};
});
